const app = getApp()  //获取应用实例

Page({
    data: {
        // uploaderList:已选择的图片临时路径数组
        // uploaderNum:已选择图片个数
        // showUpload:用来判断是否可继续选择图片,当传至6张时不可继续上传
        uploaderList: [],
        uploaderNum:0,
        showUpload:true,
        show:false,
        location:"请选择位置"
    },  
 
    // 一、关于文本内容：
    // 1.1 标题-医嘱：
    getTitle(e){
        console.log(e.detail)
        this.setData({
            title:e.detail.value
        })
    },

    getHospital(e){
        console.log(e.detail)
        this.setData({
            hospital:e.detail.value
        })
    },
    getDoctor(e){
        console.log(e.detail)
        this.setData({
            doctor:e.detail.value
        })
    }, 
 

    // 1.2获取textarea输入的文本内容：  
   getXiangQingValue(e){
        console.log(e.detail)
        this.setData({
            detailValue:e.detail.value
        })
   },

    // 二.关于图片
    // 2.1 删除图片
    clearImg:function(e){
        var nowList = [];//来装新数据
        var uploaderList = this.data.uploaderList;//原数据
        
        for (let i = 0; i < uploaderList.length;i++){
            if (i == e.currentTarget.dataset.index){
                continue;
            }else{
                nowList.push(uploaderList[i])
            }
        }
        this.setData({
            uploaderNum: this.data.uploaderNum - 1,
            uploaderList: nowList,
            showUpload: true
        })
        console.log("删除了一张图片后："+uploaderList)
    },

     // 2.2 展示(放大)图片
     showImg:function(e){
        var that=this;
        wx.previewImage({
            urls: that.data.uploaderList,
            current: that.data.uploaderList[e.currentTarget.dataset.index]
        })
    },
 
    // 2.3 选择图片：
    ChooseImagePhoto: function(e) {
        console.log("sjkhdla")
        var that = this;
        wx.chooseImage({
            count: 6 - that.data.uploaderNum, // 默认6
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                let tempFilePaths = res.tempFilePaths; //★★★这里拿到的是图片在微信客户端的临时路径！！！
                let uploaderList = that.data.uploaderList.concat(tempFilePaths); //将数组that.data.uploaderList与tempFilePaths组合成一个数组
                if (uploaderList.length==6){
                    that.setData({ 
                        showUpload:false
                    }) 
                }
                that.setData({
                    uploaderList: uploaderList,
                    uploaderNum: uploaderList.length,
                })
                console.log("已选中图片集合：",that.data.uploaderList);
            }
        })
    },
  
 
     //三. 右侧【提交按钮】绑定的事件--上传所有【文本】和【图片】信息
     submitData(){
        if(app.globalData.userInfo==null){
            wx.getUserProfile({
                desc: '获取用户必要的信息',
                success: (res) => {
                    this.setData({ show: false }),
                    app.globalData.userInfo=res.userInfo
                }    
            })  
        }else if(this.data.detailValue!=null)
        { 
            // 3.1 上传表单数据  + 图片 ：
            wx.cloud.database().collection("yizhu-Info").add({
                data:{  
                    nickName:app.globalData.userInfo.nickName, 
                    headImg:app.globalData.userInfo.avatarUrl,
                    detailValue:this.data.detailValue,
                    title:this.data.title,
                    images:this.data.uploaderList,
                    posttime:Date.now(),
                    hospital:this.data.hospital,
                    doctor:this.data.doctor,
                    // commentList可以当备注补充：
                    commentList:[], 
                    starList:[], 
                    idnum:app.globalData.idnum,
                    addressInfo:this.data.addressInfo,
                    latitude:this.data.latitude,
                    longitude:this.data.longitude,
                }, 
                success(res){
                    console.log("所有数据上传到云服务器成功！",res)
                    wx.navigateTo({
                      url: '/pages/yizhu/yizhu',
                    })
                }
            })
     
        }
     },

    // 四、选择当前位置
    ChooseAddress(){
       var that=this;
       wx.chooseLocation({
         success(res){
        console.log(res)
        if(res.address==""){
            that.data.setData({
                location:"请选择位置",
                addressInfo:"",
                latitude:"",
                longitude:""
            })
        }
        else{
             that.setData({
                 location:res.address+res.name,
                 addressInfo:res.address+res.name,
                 latitude:res.latitude,
                 longitude:res.longitude,
             })
         }
            }
       })
    }
  



})